#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.shortcuts import render,HttpResponse,redirect
from django.shortcuts import render_to_response,RequestContext
from django.core import serializers
from bbs import models
import json
from django.views.decorators.csrf import csrf_exempt
# Create your views here.


def index(request):
    all_data = models.News.objects.all()
    return render_to_response('bbs/index.html',{'data':all_data})
    #return render_to_response('bbs/index.html')
@csrf_exempt
def addfavor(request):
    ret = {'status':0,'data':'', 'messages':''}
    try:
        id = request.POST.get('nid')
        #print id
        newsobj = models.News.objects.get(id = id)
        print newsobj
        temp = newsobj.favor_count +1
        newsobj.favor_count =  temp
        newsobj.save()
        ret['status'] = 1
        ret['data'] = temp
        print temp
        print ret
        ret = serializers.serialize('json',ret)
    except Exception,e:
        ret['messages'] = e.message
        print e.message
        return HttpResponse(ret)
        #return HttpResponse('hello')
@csrf_exempt
def addplay(request):
    id = request.GET.get('nid')
    replay_list = models.Replay.objects.filter(new__id=id).values('id','content','create_date','user__username')
    ret = serializers.serialize('json',replay_list)
    return HttpResponse(ret)

def login(request):
    if request.method =='POST':
        user = request.POST.get('username',None)
        pwd = request.POST.get('password',None)
        print user,pwd
        if user == 'tom' and pwd == '123':
            #return redirect('/bbs/login.html')
            return render_to_response('bbs/login.html',
                                     context_instance=RequestContext(request))
        else:
            return render_to_response('bbs/index.html',{'msg':'user or passwd is wrong'},
                                      context_instance=RequestContext(request))
    return render_to_response('bbs/index.html',
                              context_instance=RequestContext(request))





